#include<bits/stdc++.h>
using namespace std;

struct student {
		char name[101];
		int grade;
	}aa[25];

bool cmp(student x,student y)
{
	if(x.grade!=y.grade) 
		return x.grade>y.grade;
	int temp=strcmp(x.name,y.name);
	if(temp!=0)
		return temp<0;
}

int main()
{
	int n;
	cin>>n;
	
	for (int i=0;i<n;i++)
	{
		cin>>aa[i].name;
		cin>>aa[i].grade;
	}
	
	stable_sort(aa,aa+n,cmp);
	
	for (int i=0;i<n;i++) 
		cout<<aa[i].name<<' '<<aa[i].grade<<endl;
	
	return 0;
}